<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- This file documents the use of the GNU compilers.

Copyright © 1988-2023 Free Software Foundation, Inc.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Funding Free Software", the Front-Cover
Texts being (a) (see below), and with the Back-Cover Texts being (b)
(see below).  A copy of the license is included in the section entitled
"GNU Free Documentation License".

(a) The FSF's Front-Cover Text is:

A GNU Manual

(b) The FSF's Back-Cover Text is:

You have freedom to copy and modify this GNU Manual, like GNU
     software.  Copies published by the Free Software Foundation raise
     funds for GNU development. -->
<title>Static Definitions (Using the GNU Compiler Collection (GCC))</title>

<meta name="description" content="Static Definitions (Using the GNU Compiler Collection (GCC))">
<meta name="keywords" content="Static Definitions (Using the GNU Compiler Collection (GCC))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">

<link href="index.html" rel="start" title="Top">
<link href="Indices.html" rel="index" title="Indices">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="C_002b_002b-Misunderstandings.html" rel="up" title="C++ Misunderstandings">
<link href="Name-lookup.html" rel="next" title="Name lookup">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
kbd.key {font-style: normal}
span:hover a.copiable-link {visibility: visible}
-->
</style>


</head>

<body lang="en_US">
<div class="subsection-level-extent" id="Static-Definitions">
<div class="nav-panel">
<p>
Next: <a href="Name-lookup.html" accesskey="n" rel="next">Name Lookup, Templates, and Accessing Members of Base Classes</a>, Up: <a href="C_002b_002b-Misunderstandings.html" accesskey="u" rel="up">Common Misunderstandings with GNU C++</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Indices.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h4 class="subsection" id="Declare-and-Define-Static-Members"><span>14.7.1 Declare <em class="emph">and</em> Define Static Members<a class="copiable-link" href="#Declare-and-Define-Static-Members"> &para;</a></span></h4>

<a class="index-entry-id" id="index-C_002b_002b-static-data_002c-declaring-and-defining"></a>
<a class="index-entry-id" id="index-static-data-in-C_002b_002b_002c-declaring-and-defining"></a>
<a class="index-entry-id" id="index-declaring-static-data-in-C_002b_002b"></a>
<a class="index-entry-id" id="index-defining-static-data-in-C_002b_002b"></a>
<p>When a class has static data members, it is not enough to <em class="emph">declare</em>
the static member; you must also <em class="emph">define</em> it.  For example:
</p>
<div class="example smallexample">
<pre class="example-preformatted">class Foo
{
  ...
  void method();
  static int bar;
};
</pre></div>

<p>This declaration only establishes that the class <code class="code">Foo</code> has an
<code class="code">int</code> named <code class="code">Foo::bar</code>, and a member function named
<code class="code">Foo::method</code>.  But you still need to define <em class="emph">both</em>
<code class="code">method</code> and <code class="code">bar</code> elsewhere.  According to the ISO
standard, you must supply an initializer in one (and only one) source
file, such as:
</p>
<div class="example smallexample">
<pre class="example-preformatted">int Foo::bar = 0;
</pre></div>

<p>Other C++ compilers may not correctly implement the standard behavior.
As a result, when you switch to <code class="command">g++</code> from one of these compilers,
you may discover that a program that appeared to work correctly in fact
does not conform to the standard: <code class="command">g++</code> reports as undefined
symbols any static data members that lack definitions.
</p>

</div>



</body>
</html>
